<template>
  <div class="crv-table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="dataList"
      style="width: 100%"
      :row-key="tableConfig.rowkey"
      border
      :tree-props="{
        children: tableConfig.treeChildren,
        hasChildren: tableConfig.hasChildren
      }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        :align="align"
        v-if="tableConfig.showSelection"
        min-width="50"
      ></el-table-column>
      <el-table-column
        type="index"
        label="序号"
        v-if="tableConfig.showIndex"
        :align="align"
        min-width="80"
      ></el-table-column>
      <el-table-column
        :align="align"
        :prop="propItem.prop"
        :label="propItem.label"
        :min-width="propItem.minWidth"
        v-for="propItem in tableConfig.tableProp"
        :fixed="propItem.fixed"
        show-overflow-tooltip
        :key="propItem.prop"
      >
        <template #default="scope">
          <slot :name="propItem.slotName" :row="scope.row">
            {{ scope.row[propItem.prop] }}
          </slot>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { ITable } from '../types'

export default defineComponent({
  props: {
    title: {
      type: String,
      default: ''
    },
    dataList: {
      type: Array,
      required: true
    },
    tableConfig: {
      type: Object as PropType<ITable>,
      required: true
    },
    showFooter: {
      type: Boolean,
      default: true
    },
    listCount: {
      type: Number,
      default: 0
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 10 })
    },
    align: {
      type: String,
      default: () => 'center'
    }
  },
  emits: ['selectionChange', 'update:page'],
  setup(props, { emit }) {
    const handleSelectionChange = (value: any) => {
      emit('selectionChange', value)
    }

    const handleCurrentChange = (currentPage: number) => {
      emit('update:page', { ...props.page, currentPage })
    }

    const handleSizeChange = (pageSize: number) => {
      emit('update:page', { ...props.page, pageSize })
    }

    return {
      handleSelectionChange,
      handleCurrentChange,
      handleSizeChange
    }
  }
})
</script>

<style scoped lang="less">
.crv-table {
  border-top: 20px solid #f0f2f5;
  padding: 10px;
  .header {
    display: flex;
    height: 45px;
    padding: 0 5px;
    justify-content: space-between;
    align-items: center;

    .title {
      font-size: 20px;
      font-weight: 700;
    }

    .handler {
      align-items: center;
    }
  }

  .footer {
    margin-top: 15px;
    .el-pagination {
      display: flex;
      justify-content: right;
    }
  }
}
</style>
